package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.listener.classification.ClassificationDataImportListener;
import com.ruoyi.quality.domain.bo.QualityClassificationDataBo;
import com.ruoyi.quality.domain.vo.QualityClassificationDataImportVo;
import com.ruoyi.quality.domain.vo.QualityClassificationDataVo;
import com.ruoyi.quality.domain.vo.QualityClassificationUserData;
import com.ruoyi.quality.service.IQualityClassificationDataService;
import com.ruoyi.quality.service.IQualityUserClassificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 分级得分
 *
 * @author ruoyi
 * @date 2023-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/classificationData")
public class QualityClassificationDataController extends BaseController {

    private final IQualityClassificationDataService iQualityClassificationDataService;

    private final IQualityUserClassificationService iQualityUserClassificationService;

    /**
     * 查询分级得分列表
     */
    @SaCheckPermission("quality:classificationData:list")
    @GetMapping("/list")
    public TableDataInfo<QualityClassificationDataVo> list(QualityClassificationDataBo bo, PageQuery pageQuery) {
        return iQualityClassificationDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询分级得分列表
     */
    @SaCheckPermission("quality:classificationData:list")
    @GetMapping("/listUser")
    public List<QualityClassificationUserData> listUser(QualityClassificationDataBo bo) {
        return iQualityClassificationDataService.queryUserList(bo);
    }

    /**
     * 查询分级得分列表
     */
    @SaCheckPermission("quality:classificationData:list")
    @GetMapping("/listProgress")
    public List<QualityClassificationUserData> listProgress(QualityClassificationDataBo bo) {
        return iQualityClassificationDataService.queryProgressList(bo);
    }

    /**
     * 导出全部分级得分列表
     */
    @SaCheckPermission("quality:classificationData:export")
    @Log(title = "分级得分", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(QualityClassificationDataBo bo, HttpServletResponse response) {
        List<QualityClassificationDataVo> list = iQualityClassificationDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "分级得分", QualityClassificationDataVo.class, response);
    }

    /**
     * 导出选择分级得分列表
     */
    @SaCheckPermission("quality:classificationData:export")
    @Log(title = "分级得分", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{ids}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> ids, HttpServletResponse response) {
        List<QualityClassificationDataVo> list = iQualityClassificationDataService.queryByIds(ids);
        ExcelUtil.exportExcel(list, "分级得分", QualityClassificationDataVo.class, response);
    }

    /**
     * 获取分级得分详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:classificationData:query")
    @GetMapping("/{id}")
    public R<QualityClassificationDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iQualityClassificationDataService.queryById(id));
    }

    /**
     * 新增分级得分
     */
    @SaCheckPermission("quality:classificationData:add")
    @Log(title = "分级得分", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityClassificationDataBo bo) {
        iQualityClassificationDataService.insertByBo(bo);
        return toAjax(iQualityUserClassificationService.updatePracticeByDataBo(bo));
    }

    /**
     * 修改分级得分
     */
    @SaCheckPermission("quality:classificationData:edit")
    @Log(title = "分级得分", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityClassificationDataBo bo) {
        iQualityClassificationDataService.updateByBo(bo);
        return toAjax(iQualityUserClassificationService.updatePracticeByDataBo(bo));
    }

    /**
     * 删除分级得分
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:classificationData:remove")
    @Log(title = "分级得分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        iQualityClassificationDataService.deleteWithValidByIds(Arrays.asList(ids), true);
        Boolean flag = iQualityUserClassificationService.deleteTotalByDataIds(Arrays.asList(ids));
        return toAjax(flag);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "项目得分",  QualityClassificationDataVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("quality:classificationData:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<QualityClassificationDataImportVo> result = ExcelUtil.importExcel(file.getInputStream(), QualityClassificationDataImportVo.class, new ClassificationDataImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
}
